<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

use App\Utils\JwtUtil;
use Hyperf\Context\ApplicationContext;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use HyperfExtension\Auth\Contracts\AuthManagerInterface;
use HyperfExtension\Auth\Contracts\GuardInterface;
use HyperfExtension\Auth\Contracts\StatefulGuardInterface;
use HyperfExtension\Auth\Contracts\StatelessGuardInterface;
use Psr\Container\ContainerInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

if (!function_exists('auth')) {
    /**
     * Auth认证辅助方法.
     * @param string $guard 守护名称
     * @return GuardInterface|StatefulGuardInterface|StatelessGuardInterface
     */
    function auth(string $guard = 'api'): GuardInterface|StatefulGuardInterface|StatelessGuardInterface
    {
        if (is_null($guard)) {
            $guard = config('auth.default.guard');
        }
        return \Hyperf\Support\make(AuthManagerInterface::class)->guard($guard);
    }
}
if (!function_exists('make')) {
    /**
     * @param string $name
     * @param array $parameters
     * @return mixed
     */
    function make(string $name, array $parameters = []): mixed
    {
        return \Hyperf\Support\make($name, $parameters);
    }
}

if (!function_exists('container')) {
    /**
     * 获取容器实例.
     */
    function container(): ContainerInterface
    {
        return ApplicationContext::getContainer();
    }
}

if (!function_exists('userID')) {
    /**
     * 获取当前登录用户实例.
     */
    function userID(): int
    {
        $payload = make(JwtUtil::class)->getPayload()->toArray();

        return intval($payload['sub'] ?? 0);
    }
}

if (!function_exists('toTree')) {
    /**
     * 列表转子父结构.
     * @param array $list
     * @param string $field
     * @param string $child
     * @param int $parentId
     * @return array
     */
    function toTree(array $list, $field = 'parent_id', $child = 'child', $parentId = 0): array
    {
        $arr = [];

        foreach ($list as $value) {
            if ($value[$field] === $parentId) {
                $value[$child] = toTree($list, $field, $child, $value['id']);
                $arr[] = $value;
            }
        }

        return $arr;
    }
}

if (!function_exists('co')) {
    /**
     * @param callable $callable
     * @return bool|int
     */
    function co(callable $callable)
    {
        return \Hyperf\Coroutine\co($callable);
    }
}

if (!function_exists('env')) {
    /**
     * @param $key
     * @param string|null $default
     * @return mixed
     */
    function env($key, $default = null): mixed
    {
        return \Hyperf\Support\env($key, $default);
    }
}

if (!function_exists('event')) {
    /**
     * @param object $event
     * @return null|array|bool|mixed|string
     */
    function event(object $event)
    {
        return container()->get(EventDispatcherInterface::class)->dispatch($event);
    }
}

if (!function_exists('config')) {
    /**
     * @param string $key
     * @param string|null $default
     * @return mixed
     */
    function config(string $key, $default = null)
    {
        return \Hyperf\Config\config($key, $default);
    }
}

if (!function_exists('img_url')) {
    function img_url(string $path): string
    {
        if (str_contains($path, 'http') || str_contains($path, 'https')) {
            return $path;
        }
        return 'http://' . env('QINBIU_DOMAIN') . '/' . $path;
    }
}

if (!function_exists('validator')) {
    function validator(array $data, array $rules, array $messages = [], array $customAttributes = []): string
    {
        $validationFactory = container()->get(ValidatorFactoryInterface::class);
        $validator = $validationFactory->make(
            $data,
            $rules,
            $messages,
            $customAttributes,
        );

        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        return '';
    }
}
